package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.manager.HbAppManager;
import shuashuami.hb.com.share.ShareCUserInfo;

import static android.R.attr.tag;

public class CPerfectDataActivity extends AbActivity {

    private EditText edtPassword, edtUserName, edtRealName, edtQQ, edtEmail, edtRecommenderId, edtAgentNum, edtSHenfenzheng;
    private Button btnRegistered;

    private LinearLayout llDaili, llTuijianrne, llSHenfenzheng;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_cperfect_data);
        edtPassword = (EditText) findViewById(R.id.edt_cperfect_data_password);
        edtUserName = (EditText) findViewById(R.id.edt_cperfect_data_user_name);
        edtRealName = (EditText) findViewById(R.id.edt_cperfect_data_real_name);
        edtQQ = (EditText) findViewById(R.id.edt_cperfect_data_qq);
        edtEmail = (EditText) findViewById(R.id.edt_cperfect_data_eamil);
        edtRecommenderId = (EditText) findViewById(R.id.edt_cperfect_data_recommender_number);
        edtAgentNum = (EditText) findViewById(R.id.edt_cperfect_data_agent_num);
        btnRegistered = (Button) findViewById(R.id.btn_cperfect_data_registered);
        edtSHenfenzheng = (EditText) findViewById(R.id.edt_cperfect_data_shenfenzheng);
        llDaili = (LinearLayout) findViewById(R.id.ll_daili);
        llTuijianrne = (LinearLayout) findViewById(R.id.ll_tujianren);

        llSHenfenzheng = (LinearLayout) findViewById(R.id.ll_shenfenzheng);
    }

    private String phone, hasAgentnum = "";

    @Override
    protected void initData() {
        setTitleBar("完善信息");
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        hasAgentnum = intent.getStringExtra("hasAgentnum");
        if (hasAgentnum != null) {
            if (hasAgentnum.equals("1")) {
                llTuijianrne.setVisibility(View.GONE);
            } else {
                llDaili.setVisibility(View.GONE);
            }
        } else {
            hasAgentnum = "";
        }
        int tag = HbAppManager.getInstance().getTypeValue();
        if (tag == 2 || tag == 3) {
            llSHenfenzheng.setVisibility(View.VISIBLE);
            llTuijianrne.setVisibility(View.GONE);
            llDaili.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListener() {
        btnRegistered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkAccount()) {
                    perfectData();
                }
            }
        });
    }

    public void perfectData() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        String phone = shareCUserInfo.getPhoneNum();
        String sn = shareCUserInfo.getwx_sn();
        HttpMethods.cPerfectData(listener,
                phone, edtPassword.getText().toString(),
                edtPassword.getText().toString(), edtQQ.getText().toString(), edtEmail.getText().toString(),
                edtRecommenderId.getText().toString(), edtAgentNum.getText().toString(), sn, hasAgentnum
                , edtUserName.getText().toString(), edtSHenfenzheng.getText().toString());
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(CPerfectDataActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject temp = jsonObject.getJSONObject("content");
                            ShareCUserInfo shareCUserInfo = new ShareCUserInfo(CPerfectDataActivity.this);
                            int tag = HbAppManager.getInstance().getTypeValue();
                            if (tag == 3) {
                                shareCUserInfo.setId(temp.getString("shopId"));
                            }
                            if (tag == 1) {
                                shareCUserInfo.setId(temp.getString("brushId"));
                            }
                            if (tag == 2) {
                                shareCUserInfo.setId(temp.getString("agentId"));
                            }
                            shareCUserInfo.setPassword(edtPassword.getText().toString());
                            ToastUtil.showShort(CPerfectDataActivity.this, jsonObject.getString("message"));
                            if (tag == 1) {
                                weiChartLogin(shareCUserInfo.getwx_sn(), shareCUserInfo.getUserAvatar());
                            } else if (tag == 2) {
                                openActivity(HbLoginAct.class);
                            } else {
                                login();
                            }
                        } else {
                            ToastUtil.showShort(CPerfectDataActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

                case 2:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(CPerfectDataActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) { //成功
                            ShareCUserInfo shareCUserInfo = new ShareCUserInfo(CPerfectDataActivity.this);
                            JSONObject temp = jsonObject.getJSONObject("content");
                            int tag = HbAppManager.getInstance().getTypeValue();
                            if (tag == 3) {
                                shareCUserInfo.setId(temp.getString("shopId"));
                            }
                            if (tag == 1) {
                                shareCUserInfo.setId(temp.getString("brushId"));
                            }
                            if (tag == 2) {
                                shareCUserInfo.setId(temp.getString("agentId"));
                            }
                            openActivity(HbMainAct.class);
                            finish();
                        } else {
                            openActivity(HbLoginAct.class);
                            ToastUtil.showShort(CPerfectDataActivity.this, msg.obj.toString());
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(CPerfectDataActivity.this, "数据解析错误");
                    }
                    break;

                case 3:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(CPerfectDataActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) { //成功
                            ShareCUserInfo shareCUserInfo = new ShareCUserInfo(CPerfectDataActivity.this);
                            HbAppManager.getInstance().setLoginType(tag);
                            shareCUserInfo.setTag(tag + "");
                            openActivity(HbMainAct.class);
                            finish();
                        } else {
                            openActivity(HbLoginAct.class);
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(CPerfectDataActivity.this, "数据解析错误");
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void weiChartLogin(String wxsn, String headimg) {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(3);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };

        HttpMethods.weichartLogin(listener,
                wxsn, headimg
        );
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        shareCUserInfo.setwx_sn(wxsn);
        shareCUserInfo.setUserAvatar(headimg);
    }

    private void login() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        //显示ProgressDialog
        HttpMethods.login(listener,
                phone, edtPassword.getText().toString()
        );
    }

    private boolean checkAccount() {
        if (edtPassword.getText().toString().trim().isEmpty()) {
            edtPassword.setError("密码不能为空");
            edtPassword.requestFocus();
            return false;
        }
        if (edtUserName.getText().toString().trim().isEmpty()) {
            edtUserName.setError("用户名不能为空");
            edtUserName.requestFocus();
            return false;
        }
        if (edtRealName.getText().toString().trim().isEmpty()) {
            edtRealName.setError("姓名不能为空");
            edtRealName.requestFocus();
            return false;
        }
        return true;
    }
}
